/*
   (C) 2007 - Michael Ocean - Boston University

  This work is a part of the larger snBench project.  Information
  can be found at: http://csr.bu.edu/snbench

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package sxe.sensors;

import http.RawResult;
import http.Status;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import javax.imageio.ImageIO;

import snobject.snBoolean;
import snobject.snImage;
import snobject.snObject;

public class PTZSensor extends GenericSensor {

	private String host;
	private String location;

	/** no one should call this constructor other than the Parser for use by the SSD/SRM!! */
	protected PTZSensor(){
		super();
	}
	
	public PTZSensor(String host, String id, String location) {
		m_state = STATE_PREINIT;
		m_type = TYPE_IMAGE_PTZ;

		this.host = host;
		super.m_id = id;
		this.location = location;
		
		// m_uri = HOST + PREFIX + TYPE + ID;
		super.m_uri = m_type + "/" + m_id;
		SensorDetector.RegisterID(m_id);
	}


	@Override
	public String GetInfoXML() {
		// put specific info here
		String s = new String("<location>" + location + "</location>");
		
		return super.GetInfoXML(s);
	}

	@Override
	public snObject getData(Object oParam) {
		// http://192.168.0.103/axis-cgi/jpg/image.cgi?resolution=320x240
		String extraparams = "";
		if(oParam!=null)
			extraparams = (String)oParam;
		
		return DumbStringGetter("http://" + host + "/axis-cgi/jpg/image.cgi?resolution=320x240" + extraparams);
	}

	@Override
	public boolean initializeSensor() {
		InetAddress address = null;
		// ping the sensor
	     try {
	         address = InetAddress.getByName(host);
	         if(address.isReachable(3000)){
	 			m_state = STATE_INIT;
				return true;
	         }
	     }
	     catch (IOException e) {
	        System.err.println("Unable to reach PTZ camera at " + host);
	     }
		m_state = STATE_FAILED;
		return false;
	
	}
	@Override
	public boolean isAlive() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean shutDown() {
		// easy for stateless connections :)
		return true;
	}


	public snObject pan(int amount) 
	{
		// http://192.168.0.103/axis-cgi/jpg/image.cgi?resolution=320x240
			
		return new snBoolean(HTTPAction("http://" + host + "/axis-cgi/com/ptz.cgi?pan="+amount));
	}

	
	/* For testing purposes */
	static private boolean HTTPAction(String uri){
		try {
			URI u = new URI(uri);
			RawResult rr = http.Client.GET_raw(u);
			System.out.println(rr.getType());
			System.out.println(new String(rr.getContents()));
			     
			return true; //(?)
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Status e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	/* For testing purposes */
	static public snImage DumbStringGetter(String uri){
		try {
			URI u = new URI(uri);
			RawResult rr = http.Client.GET_raw(u);
			//System.out.println(rr.getType());
			ByteArrayInputStream bais = new ByteArrayInputStream(rr.getContents());
			BufferedImage bufferedImage = ImageIO.read(bais);
			     
			snImage snI = new snImage(bufferedImage);
			return snI;
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Status e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	static public void main(String args[]) {
		try {
			URI u = new URI(args[0]);
			RawResult rr = http.Client.GET_raw(u);
			System.out.println(rr.getType());
			ByteArrayInputStream bais = new ByteArrayInputStream(rr.getContents());
			BufferedImage bufferedImage = ImageIO.read(bais);
			     
			snImage snI = new snImage(bufferedImage);

		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Status e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}
}
